// pages/home/home.js
const app = getApp()
const getUserInfo = require('request/api/index').getUserInfo
const getJgfbPage = require('request/api/quotation').getJgfbPage
const parseTime = require('utils/index').parseTime

Page({

  /**
   * 页面的初始数据
   */
  data: {
    sourceUrl: app.sourceUrl,
    fileHost: app.fileHost,
    pageNum: 1,
    pageSize: 10,
    total: 0,
    list: [],
    today: parseTime(new Date(), '{y}-{m}-{d}')
  },

  setMessage() {
    wx.requestSubscribeMessage({
      tmplIds: [
        'TeQjk5a5XmMQxHpwBPrIZlfwHz5v0pN-DA-CEHVKxLY',
        'N6SwPysDpsiUzETWqzAxprSMrYLHooFeKdn-PZ4IP1g'
      ],
      success: (res) => {
        console.log(res)
      },
      fail: (err) => {
        console.log(err)
      }
    })
  },

  getList() {
    getJgfbPage({
      pageNo: this.data.pageNum,
      pageSize: this.data.pageSize,
      // sj: this.data.today
    }).then(res => {
      res.data.list.forEach(item => {
        item.sj = parseTime(item.sj)
      })
      this.setData({
        list: this.data.list.concat(res.data.list),
        total: res.data.total,
        pageNum: this.data.pageNum + 1
      })
    })
  },

  loadList() {
    if (this.data.total <= this.data.pageNum * this.data.pageSize) {
      return
    }
    this.setData({
      pageNum: this.data.pageNum + 1
    })
    this.getList()
  },

  goToDetail (e) {
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.name
    wx.navigateTo({
      url: '/pages/quotation/detail/detail?id=' + id + '&name=' + name,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    app.resetUserInfo = (callBack) => {
      getUserInfo().then(res => {
        app.globalData.userInfo = res.data
        app.globalData.loginState = true
        if (callBack) {
          callBack()
        }
      })
    }
    let that = this
    wx.getStorage({
      key: 'token',
      success(res) {
        let token = res.data
        if (token) {
          token = JSON.parse(res.data)
          for (let k in token) {
            app.globalData[k] = token[k]
          }
          app.resetUserInfo(that.getList)
        } else {
          this.getList()
        }
      }
    })

    wx.getSetting({
      withSubscriptions: true,
      success(res) {
        console.log(res.subscriptionsSetting)
        if (!res.subscriptionsSetting.itemSettings) {
          wx.showModal({
            title: '订阅消息',
            content: '订阅消息后，系统将提醒您查看商户每日收购价格',
            success(res) {
              if (res.confirm) {
                that.setMessage()
              }
            }
          })
        }
        // res.subscriptionsSetting = {
        //   mainSwitch: true, // 订阅消息总开关
        //   itemSettings: {   // 每一项开关
        //     SYS_MSG_TYPE_RANK: 'accept'
        //     zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: 'reject', // 普通一次性订阅消息
        //     ke_OZC_66gZxALLcsuI7ilCJSP2OJ2vWo2ooUPpkWrw: 'ban',
        //   }
        // }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (app.globalData.loginState) {
      this.getList()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.loadList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})